/**************************************************************************************
* Component of simpleRTJ regression tests.
**************************************************************************************
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Created by:
*       RTJ Computing
**************************************************************************************/
class TestCastInstanceOf
{
    interface IFace
    {
        final static int i1 = 3;
        int i();
    }

    class Base
    {
        final static int b1 = 5;
        int m()
        {
            return b1;
        }
    }

    class Child extends Base
    {
        final static int c1 = 6;
        int mm()
        {
            return c1;
        }
    }

    class Child2 implements IFace
    {
        final static int v1 = 2;
        int m()
        {
            return i1;
        }
        public int i()
        {
            return v1;
        }
    }


    void test() throws Exception
    {
        Object ch = new Child();
        Object ch2 = new Child2();
        Object oarr = new byte[10];

        // instanceof bytecode
        if (ch instanceof Base) ;
        else throw new Exception("IOF1");

        if (ch instanceof Child) ;
        else throw new Exception("IOF2");

        if (ch instanceof IFace)
            throw new Exception("IOF3");

        if (ch instanceof Child2)
            throw new Exception("IOF4");

        if (ch instanceof char[])
            throw new Exception("IOF5");

        if (ch2 instanceof Child2) ;
        else throw new Exception("IOF6");

        if (ch2 instanceof IFace) ;
        else throw new Exception("IOF7");

        if (ch2 instanceof Base)
            throw new Exception("IOF8");

        if (ch2 instanceof int[])
            throw new Exception("IOF9");

        if (oarr instanceof byte[]) ;
        else throw new Exception("IOF10");

        if (oarr instanceof int[])
            throw new Exception("IOF11");

        if (oarr instanceof IFace)
            throw new Exception("IOF12");

        if (oarr instanceof Base)
            throw new Exception("IOF13");

        // checkcast bytecode
        try
        {
            Child c = (Child)ch;
            if (c.mm() != 6 || c.m() != 5)
                throw new Exception("IOF14");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF14a");
        }

        try
        {
            Base c = (Child)ch;
            if (c.m() != 5)
                throw new Exception("IOF15");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF15a");
        }

        try
        {
            Base c = (Base)ch;
            if (c.m() != 5)
                throw new Exception("IOF16");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF16a");
        }

        try
        {
            IFace f = (IFace)ch;
            throw new Exception("IOF17");
        }
        catch (ClassCastException e) {}

        try
        {
            Child2 c = (Child2)ch;
            throw new Exception("IOF18");
        }
        catch (ClassCastException e) {}

        try
        {
            IFace f = (IFace)ch2;
            if (f.i() != 2)
                throw new Exception("IOF19");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF19a");
        }

        try
        {
            IFace f = (Child2)ch2;
            if (f.i() != 2)
                throw new Exception("IOF20");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF20a");
        }

        try
        {
            Child2 c = (Child2)ch2;
            if (c.i() != 2 || c.m() != 3)
                throw new Exception("IOF21");
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF21a");
        }

        try
        {
            Object o = (char[])oarr;
            throw new Exception("IOF22");
        }
        catch (ClassCastException e) {}

        try
        {
            char ca[] = (char[])oarr;
            throw new Exception("IOF23");
        }
        catch (ClassCastException e) {}

        try
        {
            byte ba[] = (byte[])oarr;
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF24");
        }

        try
        {
            Object o = (byte[])oarr;
        }
        catch (ClassCastException e)
        {
            throw new Exception("IOF25");
        }
    }
}